#! /bin/sh

VERSION=2.0
OWNER=${OWNER:-root}
GROUP=${GROUP:-root}

INSTALLBIN="install -g $GROUP -o $OWNER -m 755"
INSTALLDOC="install -g $GROUP -o $OWNER -m 644"

if [ -z $1 ]; then
  echo "Usage: installit <prefix>"
  echo "You will want to set the following environment variables:"
  echo ""
  echo "    Name     Description (Default)"
  echo "    ─────────────────────────────────"
  echo "    TEXDIR   Your texmf-local directory (<prefix>/share/texmf-local)"
  echo "    DOCDIR   Documentation (<prefix>/doc/chezweb-$VERSION)"
  echo "    BINDIR   Binaries (<prefix>/bin)"
  echo "    LIBDIR   Libraries (<prefix>/lib/chezweb)"
  exit 1
fi

PREFIX=$1
TEXDIR=${TEXDIR:-$PREFIX/share/texmf-local}
DOCDIR=${DOCDIR:-$PREFIX/doc/chezweb-$VERSION}
BINDIR=${BINDIR:-$PREFIX/bin}
LIBDIR=${LIBDIR:-$PREFIX/lib/chezweb}
TEXSUB=${TEXDIR}/tex/generic

echo "Installing to $PREFIX"
echo "Tex Macros installed to $TEXDIR"
echo "Documentation installed to $DOCDIR"
echo "Binaries installed to $BINDIR"
echo "Libraries installed to $LIBDIR"

set -e 

echo -n "Making directories..."
mkdir -p $TEXSUB $DOCDIR $BINDIR $LIBDIR
echo "Done."

# Copy documentation
echo -n "Installing documentation..."
$INSTALLDOC README chezweb.pdf doc/cwebman.pdf $DOCDIR
echo "Done."

# Install libraries
echo -n "Installing libraries..."
if [ -e runtime.so ]; then 
  $INSTALLDOC runtime.so $LIBDIR
fi
$INSTALLDOC runtime.sls $LIBDIR
$INSTALLDOC runtime.ss $LIBDIR
$INSTALLDOC chezweb.ss $LIBDIR
echo "Done."

# Install binaries
echo -n "Installing binaries..."
$INSTALLBIN chezweave cheztangle $BINDIR
echo "Done."

# Install tex macros
echo -n "Installing tex macros..."
$INSTALLDOC chezwebmac.tex $TEXSUB
echo "Done."
